<?php
/**
 * Created by PhpStorm.
 * User: 86158
 * Date: 2020/8/1
 * Time: 15:33
 */

namespace app\merchant\controller\shop;


use app\merchant\controller\AuthController;
use app\merchant\model\order\StoreOrder;
use app\merchant\model\system\Express;
use app\models\shop\MerchantAdmin;
use app\Request;
use crmeb\services\UtilService;

class Statistics
{
    public function __construct(Request $request)
    {
        $token = $request->header('Authori-zation');
        $this->user = AuthController::check($token);
    }

    /**
     * 首页数据
     * @param Request $request
     * @return mixed
     */
    public function index(Request $request)
    {
        $time = $request->param('time','today');
        $status = $request->param('status',0);
        $page = $request->param('page',1);
        $limit = $request->param('limit',10);
        $mer_id = $this->user->id;
        $where = [
            'page'  => $page,
            'limit' => $limit,
            'order' => '',
            'mer_id'=>$mer_id,
        ];
        // 订单状态
        switch ($status) {
            case -1 :   // 维权订单
                $where['status'] = -1;
                break;
            case 0 :    // 待付款
                $where['paid'] = 0;
                break;
            case 1 :    // 待发货
                $where['status'] = 1;
                break;
            default :   //待发货
                $where['status'] = 1;
        }
        //成交量
        $data['total_money'] = StoreOrder::whereTime('pay_time', $time)->where('paid',1)->sum('pay_price');
        //付款订单
        $data['paid_order'] = StoreOrder::whereTime('pay_time', $time)->where('paid',1)->count();
        //付款商品数量
        $data['paid_product'] = StoreOrder::whereTime('pay_time', $time)->where('paid',1)->sum('total_num');
        //付款会员数
        $data['paid_user'] = StoreOrder::whereTime('pay_time', $time)->where('paid',1)->count('DISTINCT uid');
        //待发货
        $data['wait_deliver'] = StoreOrder::where('paid',1)->where('status',0)->count();
        // 未支付
        $data['no_pay'] = StoreOrder::where('paid',1)->where('status',0)->count();
        //维权订单
        $data['refund'] = StoreOrder::where('status',-1)->count();
        //列表
        $data['list'] = StoreOrder::OrderList($where);
        return app('json')->successful($data);
    }

    // 订单列表
    public function list(Request $request)
    {
        $status = $request->param('status',0);
        $mer_id = $this->user->id;
        $page = $request->param('page',1);
        $limit = $request->param('limit',10);
        $orderNo = $request->param('orderNo','');
        if($orderNo){
            $where['order_id'] = $orderNo;
        }
        $where['mer_id'] = $mer_id;
        switch ($status){
            case 0:     //待付款
                $where['paid'] = 0;
                break;
            case 1 :    //待发货
                $where['status'] = 0;
                break;
            case 2 :    //已发货
                $where['status'] = 1;
                break;
            case 3 :    //已完成
                $where['status'] = 3;
                break;
            case 4 :    //关闭订单
                $where['is_del'] = 1;
                break;
        }
        $where['page'] = $page;
        $where['limit'] = $limit;
        $where['order'] = '';
        $data = StoreOrder::OrderList($where);
        return app('json')->successful($data);
    }

    /**
     * 发货
     * @param Request $request
     * @return mixed
     */
    public function delivery(Request $request)
    {
        $list = UtilService::postMore([
            ['order_id', ''],
            ['logistics', ''],
            ['type', 1],
            ['logistics_num', ''],
            ['mark', '']
        ], $request);
        $data['shipping_type']  = $list['type'];
        $data['delivery_id']    = $list['logistics_num'];   //物流单号
        $data['logistics_id']   = $list['logistics'];       //物流公司id
        $data['remarks']        = $list['mark'];            //备注
        $data['status']         = 1;
        $res = StoreOrder::where('id',$list['order_id'])->update($data);
        if($res){
            return app('json')->successful();
        }else{
            return app('json')->fail('发货失败');
        }
    }

    /**
     * 快递公司列表
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function company()
    {
        $list = Express::where('is_show',1)->select()->toArray();
        if($list){
            return app('json')->successful($list);
        }else{
            return app('json')->fail('发货失败');
        }
    }

    public function userInfo(Request $request)
    {
        $user = MerchantAdmin::where('id', $this->user->id)->find();
        if($user){
            return app('json')->success($user->toArray());
        }else{
            return app('json')->fail('获取信息失败');
        }
    }

    public function changePwd(Request $request)
    {
        $list = UtilService::postMore([
            ['pwd', ''],
            ['password', '']
        ], $request);
        $user = MerchantAdmin::where('id', $this->user->id)->find();
        if(!$user){
            return app('json')->fail('账号不存在');
        }
        if(md5($list['pwd']) != $user['pwd']){
            return app('json')->fail('原始密码错误');
        }
        if (strlen(trim($list['password'])) < 6 || strlen(trim($list['password'])) > 16)
            return app('json')->fail('密码必须是在6到16位之间');
        if ($list['password'] == '123456') return app('json')->fail('密码太过简单，请输入较为复杂的密码');
        $res = MerchantAdmin::where('id', $this->user->id)->update(['pwd'=>md5($list['password'])]);
        if($res){
            return app('json')->success();
        }else{
            return app('json')->fail('修改密码失败');
        }
    }

}